<template>
  <div class="user-frame-layout">
    <!-- 精致侧边栏 -->
    <div class="user-sidebar">
      <div class="sidebar-header">
        <div class="logo-container">
          <div class="logo-icon">👤</div>
          <span class="logo-text">个人中心</span>
        </div>
      </div>

      <!-- 个人信息特殊菜单 -->
      <div class="sidebar-section premium-section">
        <router-link to="/userframe/usermain" class="premium-link">
          <div class="premium-icon">✨</div>
          <span>个人信息</span>
          <div class="premium-arrow">→</div>
        </router-link>
      </div>

      <div class="sidebar-section" v-for="(section, index) in sections" :key="index">
        <div class="section-header" @click="toggleSection(index)" :class="{ active: section.isOpen }">
          <div class="section-icon">{{ getSectionIcon(section.title) }}</div>
          <span class="section-title">{{ section.title }}</span>
          <div class="expand-icon" :class="{ rotated: section.isOpen }">
            <svg viewBox="0 0 24 24" width="14" height="14">
              <path d="M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z" />
            </svg>
          </div>
        </div>
        <div class="section-content" :class="{ expanded: section.isOpen }">
          <div class="content-wrapper">
            <div v-for="(item, itemIndex) in section.items" :key="itemIndex" class="menu-item">
              <router-link :to="item.route" class="menu-link">
                <div class="link-indicator"></div>
                <span>{{ item.text }}</span>
              </router-link>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 主内容区域 -->
    <div class="main-area">
      <!-- 顶部导航 -->
      <div class="top-navbar">
        <div class="navbar-container">
          <div class="user-profile">
            <div class="avatar-wrapper">
              <div class="avatar-frame">
                <img :src="user.avatar" alt="用户头像" class="user-avatar" />
              </div>
              <div class="status-indicator"></div>
            </div>
            <div class="profile-info">
              <div class="username">{{ user.username }}</div>
              <div class="user-id">ID: {{ user.account }}</div>
            </div>
          </div>

          <div class="stats-panel">
            <div class="stat-card" v-for="(stat, index) in userStats" :key="index">
              <div class="stat-number">{{ stat.value }}</div>
              <div class="stat-text">{{ stat.label }}</div>
            </div>
          </div>

          <div class="nav-actions">
            <button class="nav-btn home-btn" @click="goToHome">
              <span class="btn-icon">🏠</span>
              <span>首页</span>
            </button>
            <button class="nav-btn logout-btn" @click="logout">
              <span class="btn-icon">🚪</span>
              <span>退出</span>
            </button>
          </div>
        </div>
      </div>

      <!-- 内容展示区 -->
      <div class="content-area">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'UserFrame',
  data() {
    return {
      user: {
        avatar: '',
        username: '',
        account: '',
        likes: 0,
        follows: 0,
        fans: 0
      },
      sections: [
        {
          title: '我的点赞',
          isOpen: false,
          items: [
            { text: '攻略', route: '/userframe/like/strategy' },
            { text: '景点', route: '/userframe/like/scenic' },
          ],
        },
        {
          title: '我的收藏',
          isOpen: false,
          items: [
            { text: '攻略', route: '/userframe/collec/strategy' },
            { text: '景点', route: '/userframe/collec/scenic' },
            { text: '酒店', route: '/userframe/collec/hotel' },
            { text: '跟团游', route: '/userframe/collec/grouptour' },
          ],
        },
        {
          title: '我的评论',
          isOpen: false,
          items: [
            { text: '攻略', route: '/userframe/comment/strategy' },
            { text: '景点', route: '/userframe/comment/scenic' },
          ],
        },
        {
          title: '我的攻略',
          isOpen: false,
          items: [
            { text: '发布攻略', route: '/userframe/strategy/new' },
            { text: '已发布', route: '/userframe/strategy/published' },
          ],
        },
        {
          title: '消息通知',
          isOpen: false,
          items: [
            { text: '收到的点赞', route: '/userframe/notifications/likes' },
            { text: '收到的关注', route: '/userframe/notifications/follows' },
            { text: '收到的评论', route: '/userframe/notifications/comments' },
            { text: '收到的收藏', route: '/userframe/notifications/collections' },
          ],
        },
      ],
    };
  },
  computed: {
    userStats() {
      return [
        { label: '获赞', value: this.user.likes },
        { label: '关注', value: this.user.follows },
        { label: '粉丝', value: this.user.fans }
      ];
    }
  },
  async mounted() {
    const loginId = localStorage.getItem('loginId');
    const account = localStorage.getItem('userAccount');
    if (loginId) {
      // 查询用户基本信息
      const res = await fetch(`http://localhost:8080/mw/UserByLoginId?loginId=${loginId}`);
      const userInfo = await res.json();
      this.user.avatar = userInfo.userAvatar || '';
      this.user.username = userInfo.userName || '';
      this.user.account = account || '';
      const userId = userInfo.userId;
      localStorage.setItem('userId', userId);

      // 1. 查询该用户发布的所有攻略ID
      let strategyIds = [];
      try {
        const strategyRes = await fetch(`http://localhost:8080/ts/listByUser?userId=${userId}`);
        const strategyList = await strategyRes.json();
        strategyIds = (strategyList || []).map(item => item.strategyId);
      } catch (e) { }

      // 2. 查询所有点赞记录，统计这些攻略收到的点赞数
      let likes = 0;
      try {
        const likeRes = await fetch(`http://localhost:8080/strlike/list`);
        const likeList = await likeRes.json();
        likes = (likeList || []).filter(item => strategyIds.includes(item.strategyId)).length;
      } catch (e) { }

      // 3. 查询关注和粉丝
      let follows = 0;
      let fans = 0;
      try {
        const followRes = await fetch(`http://localhost:8080/mw/userFollowList`);
        const followList = await followRes.json();
        follows = (followList || []).filter(item => item.userId === userId).length;
        fans = (followList || []).filter(item => item.followedUserId === userId).length;
      } catch (e) { }

      this.user.likes = likes;
      this.user.follows = follows;
      this.user.fans = fans;
    }
  },
  methods: {
    toggleSection(index) {
      if (this.sections[index].isOpen) {
        this.sections[index].isOpen = false;
      } else {
        this.sections.forEach((section, i) => {
          section.isOpen = false;
        });
        this.sections[index].isOpen = true;
      }
    },
    getSectionIcon(title) {
      const icons = {
        '我的点赞': '❤️',
        '我的收藏': '⭐',
        '我的评论': '💬',
        '我的攻略': '📝',
        '消息通知': '🔔'
      };
      return icons[title] || '📁';
    },
    goToHome() {
      this.$router.push('/');
    },
    logout() {
      localStorage.removeItem('loginId');
      localStorage.removeItem('userAccount');
      this.$router.replace('/');
    }
  }
}
</script>

<style scoped>
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.user-frame-layout {
  display: flex;
  min-height: 100vh;
  background: linear-gradient(135deg, #eeeff1 0%, #f4f3f6 100%);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* 侧边栏样式 */
.user-sidebar {
  width: 300px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-right: 1px solid rgba(255, 255, 255, 0.3);
  padding: 0;
  height: 100vh;
  position: sticky;
  top: 0;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(102, 126, 234, 0.3) transparent;
  box-shadow: 2px 0 20px rgba(0, 0, 0, 0.1);
}

.user-sidebar::-webkit-scrollbar {
  width: 4px;
}

.user-sidebar::-webkit-scrollbar-track {
  background: transparent;
}

.user-sidebar::-webkit-scrollbar-thumb {
  background: rgba(102, 126, 234, 0.3);
  border-radius: 2px;
}

.sidebar-header {
  padding: 30px 25px;
  border-bottom: 1px solid rgba(102, 126, 234, 0.1);
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05), rgba(118, 75, 162, 0.05));
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  font-size: 24px;
  color: #667eea;
}

.logo-text {
  font-size: 20px;
  font-weight: 600;
  color: #2d3748;
  letter-spacing: -0.5px;
}

.sidebar-section {
  margin: 8px 0;
}

.premium-section {
  margin: 20px 15px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 12px;
  padding: 2px;
  position: relative;
}

.premium-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 18px 20px;
  background: white;
  border-radius: 10px;
  text-decoration: none;
  color: #2d3748;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.premium-link:hover {
  background: rgba(102, 126, 234, 0.05);
  color: #667eea;
}

.premium-icon {
  font-size: 18px;
  color: #667eea;
}

.premium-arrow {
  margin-left: auto;
  font-size: 16px;
  color: #a0aec0;
  transition: all 0.3s ease;
}

.premium-link:hover .premium-arrow {
  color: #667eea;
  transform: translateX(3px);
}

.section-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 25px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #4a5568;
  font-weight: 500;
  position: relative;
}

.section-header:hover {
  background: rgba(102, 126, 234, 0.08);
  color: #2d3748;
}

.section-header.active {
  background: rgba(102, 126, 234, 0.12);
  color: #667eea;
  border-left: 3px solid #667eea;
}

.section-icon {
  font-size: 16px;
  width: 16px;
  text-align: center;
}

.section-title {
  flex: 1;
  font-size: 15px;
}

.expand-icon {
  color: #a0aec0;
  transition: all 0.3s ease;
}

.expand-icon.rotated {
  transform: rotate(90deg);
  color: #667eea;
}

.expand-icon svg {
  fill: currentColor;
}

.section-content {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.4s ease;
}

.section-content.expanded {
  max-height: 250px;
}

.content-wrapper {
  padding: 8px 0 16px 0;
}

.menu-item {
  margin: 2px 0;
}

.menu-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 25px 12px 45px;
  text-decoration: none;
  color: #718096;
  font-size: 14px;
  transition: all 0.3s ease;
  position: relative;
}

.menu-link:hover {
  background: rgba(102, 126, 234, 0.06);
  color: #4a5568;
}

.link-indicator {
  width: 4px;
  height: 4px;
  background: #cbd5e0;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.menu-link:hover .link-indicator {
  background: #667eea;
  transform: scale(1.5);
}

/* 主内容区域 */
.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.top-navbar {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.3);
  margin-bottom: 0;
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
}

.navbar-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 25px 35px;
  max-width: 1200px;
  margin: 0 auto;
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 16px;
}

.avatar-wrapper {
  position: relative;
}

.avatar-frame {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  padding: 2px;
  background: linear-gradient(135deg, #667eea, #dad3e2);
  position: relative;
}

.user-avatar {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid white;
}

.status-indicator {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 14px;
  height: 14px;
  background: #48bb78;
  border: 2px solid white;
  border-radius: 50%;
}

.profile-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.username {
  font-size: 18px;
  font-weight: 600;
  color: #2d3748;
}

.user-id {
  font-size: 13px;
  color: #718096;
}

.stats-panel {
  display: flex;
  gap: 32px;
}

.stat-card {
  text-align: center;
  padding: 16px 20px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 12px;
  border: 1px solid rgba(102, 126, 234, 0.1);
  transition: all 0.3s ease;
  min-width: 80px;
}

.stat-card:hover {
  background: rgba(255, 255, 255, 1);
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.15);
}

.stat-number {
  font-size: 24px;
  font-weight: 700;
  color: #2d3748;
  margin-bottom: 4px;
}

.stat-text {
  font-size: 12px;
  color: #718096;
  font-weight: 500;
}

.nav-actions {
  display: flex;
  gap: 12px;
}

.nav-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  font-weight: 500;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.home-btn {
  background: linear-gradient(135deg, #c7cce3, #c3aad1);
  color: white;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.logout-btn {
  background: rgba(255, 255, 255, 0.9);
  color: #4a5568;
  border: 1px solid rgba(102, 126, 234, 0.2);
}

.nav-btn:hover {
  transform: translateY(-1px);
}

.home-btn:hover {
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.logout-btn:hover {
  background: rgba(102, 126, 234, 0.1);
  border-color: rgba(102, 126, 234, 0.3);
}

.btn-icon {
  font-size: 16px;
}

.content-area {
  flex: 1;
  padding: 30px;
  background: white;
  margin: 20px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  overflow-y: auto;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .user-sidebar {
    width: 280px;
  }

  .navbar-container {
    flex-direction: column;
    gap: 20px;
    align-items: stretch;
  }

  .stats-panel {
    justify-content: center;
    gap: 20px;
  }

  .nav-actions {
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .user-sidebar {
    width: 100%;
    position: fixed;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }

  .main-area {
    width: 100%;
  }

  .content-area {
    margin: 10px;
  }
}
</style>